package com.example;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.AsyncTask;
import android.os.Handler;
import android.os.IBinder;
import android.os.Looper;
import android.os.Message;
import android.util.Log;
import android.widget.EditText;
import android.widget.Toast;

import com.example.androidhive.JSONParser;

public class AAA extends Service{

	JSONParser jsonParser = new JSONParser();
	EditText mojekontohaslo_;

	// url to create new product
	private static String url_create_product2 ="http://opiekuj1.javaprovider.net/php/rezerwa.php";

	// JSON Node names
	private static final String TAG_SUCCESS = "success";
	private static final String TAG = "MyService";
	
	//used for getting the handler from other class for sending messages
	public static Handler 		mMyServiceHandler 			= null;
	//used for keep track on Android running status
	public static Boolean 		mIsServiceRunning 			= false;

	@Override
	public IBinder onBind(Intent arg0) {
		return null;
	}

	@Override
	public void onCreate() {

		Toast.makeText(this, "MyService Created", Toast.LENGTH_LONG).show();
		Log.d(TAG, "onCreate");
	}

	@Override
	public void onStart(Intent intent, int startId) {
		Toast.makeText(this, "My Service Started", Toast.LENGTH_LONG).show();
		Log.d(TAG, "onStart");	
	}
	
	@Override
	public int onStartCommand(Intent intent, int flags, int startId) {
		
	 MyThread myThread = new MyThread();
	 myThread.start();
		
		try 
		{
			Thread.sleep(1000);
		}
		catch (InterruptedException e)
		{
			e.printStackTrace();
		}
		
		mIsServiceRunning = true; // set service running status = true
		
		Toast.makeText(this, "Congrats! My Service Started", Toast.LENGTH_LONG).show();
	//new MY().start();
		new Zegarynka().start();
		
		return START_STICKY;
	}
	public void ff(int i){
		Toast.makeText(this, "Info is "+i, Toast.LENGTH_LONG).show();
	}
	class Zegarynka extends Thread{
		public void run(){
			for (int i = 0; i<10; i++) {
				Calendar cal = Calendar.getInstance();
				  int dayofyear = cal.get(Calendar.DAY_OF_YEAR);
				  int year = cal.get(Calendar.YEAR);
				  int dayofweek = cal.get(Calendar.DAY_OF_WEEK);
				  int dayofmonth = cal.get(Calendar.DAY_OF_MONTH);
				  int monofmonth = cal.get(Calendar.MINUTE);
				  if ((monofmonth%3)!=1){
				//	  AndroidBuildingMusicPlayerActivity cc=new AndroidBuildingMusicPlayerActivity ();
				//      cc.playSong(0);
				  }
		}//
	}}
	class MY extends Thread{
		public void run(){
			for (int i = 0; i <20; i++) {
				try 
				{
					Thread.sleep(1000);
					new Czytaj().execute();
				}
				catch (InterruptedException e)
				{
					e.printStackTrace();
				}
			}
		}
	}


	class Czytaj extends AsyncTask<String,String,String> {
		@Override
		protected void onPreExecute() {
			super.onPreExecute();
		}
		protected String doInBackground(String... args) {
			List<NameValuePair> params = new ArrayList<NameValuePair>();
			params.add(new BasicNameValuePair("nazwa", "mm"));
			JSONObject json = jsonParser.makeHttpRequest(url_create_product2,
					"POST", params);
			Log.d("Create Response", json.toString());

			// check for success tag
			try {
				int success = json.getInt(TAG_SUCCESS);
				ff(success);
			} catch (JSONException e) {
				e.printStackTrace();
			}
			return null;
		}
		protected void onPostExecute(String file_url) {
			// dismiss the dialog once done
		}

	}
	class WWW extends Thread{
		MediaPlayer player;
		public void run(){
			for (int i = 0; i<10; i++) {
			Calendar cal = Calendar.getInstance();
			  int dayofyear = cal.get(Calendar.DAY_OF_YEAR);
			  int year = cal.get(Calendar.YEAR);
			  int dayofweek = cal.get(Calendar.DAY_OF_WEEK);
			  int dayofmonth = cal.get(Calendar.DAY_OF_MONTH);
			  int monofmonth = cal.get(Calendar.MINUTE);
			  if ((monofmonth%3)!=1){
				  MediaPlayer mMediaPlayer = new MediaPlayer();

				    FileInputStream fileStream = null;
				    try {
				        fileStream = new FileInputStream("/sdcard/my_sounds/TEST.mp3");
				    } catch (FileNotFoundException e1) {
				        e1.printStackTrace();
				    }

				    try {
				        mMediaPlayer.setDataSource(fileStream.getFD());
				    } catch (IllegalArgumentException e) {
				        e.printStackTrace();
				    } catch (IllegalStateException e) {
				        e.printStackTrace();
				    } catch (IOException e) {
				        e.printStackTrace();
				    }

				    try {
				        mMediaPlayer.prepare();
				    } catch (IllegalStateException e) {
				        e.printStackTrace();
				    } catch (IOException e) {
				        e.printStackTrace();
				    }
				player.start();
				//  player=MediaPlayer.create(this,R.raw.das);
				//  player.start();
				  //graj
				  //addListenerOnChkIos();
			        //we now instantiate the player using our music file;
			        //note that since it is a resource in the "raw" directory,
			        //we don't need the file's extension;
			        //our file here is in fact called "soundfile.mp3";
			   //     player = MediaPlayer.create(this, R.raw.das);
			        //we start playing the file!
			   //     player.start();
			  }
			}
		}
	}
	@Override
	public void onDestroy() {
		Toast.makeText(this, "MyService Stopped", Toast.LENGTH_LONG).show();
		Log.d(TAG, "onDestroy");
		
		mIsServiceRunning = false; // make it false, as the service is already destroyed.
	}
	
	//Your inner thread class is here to getting response from Activity and processing them
	class MyThread extends Thread
	{
		private static final String INNER_TAG = "MyThread";
		
    	public void run() 
    	{
    		this.setName(INNER_TAG);

    		// Prepare the looper before creating the handler.
			Looper.prepare();
			mMyServiceHandler = new Handler()
			{
				//here we will receive messages from activity(using sendMessage() from activity)
    			public void handleMessage(Message msg)
    			{
    				Log.i("BackgroundThread","handleMessage(Message msg)" );
    				switch(msg.what)
    				{
    				case 0: // we sent message with what value =0 from the activity. here it is
    						//Reply to the activity from here using same process handle.sendMessage()
    						//So first get the Activity handler then send the message
    				/*	if(null != ServicesDemo.mUiHandler)
    					{
    			
    						Message msgToActivity = new Message();
    						msgToActivity.what = 0; 
    						if(true ==mIsServiceRunning) {
    							msgToActivity.obj  = "Request Received. Service is Running"; // you can put extra message here
    						}
    						else
    							msgToActivity.obj  = "Request Received. Service is not Running"; // you can put extra message here
    						
    						ServicesDemo.mUiHandler.sendMessage(msgToActivity);
    					}*/
    					
    				break;
    				
					default:
						break;
    				}
				}
    		};
    		Looper.loop();
    	}
	}
}
